<?php
namespace app\service;
use think\Request,
    app\model\OrderGood,
	app\model\Product;

class PackService extends BaseService
{

    public function page(){
        // pick_status
        $data   = Request::instance()->get();
        $where  = [];

        //封装where查询条件
        // (empty($data['status'])         || $data['status'] == '')          || $where['pick_status'] = $data['status'];
        (empty($data['tally_status'])   || $data['tally_status'] == '')     || $where['tally_status'] = $data['tally_status'];
        (empty($data['out_status'])   || $data['out_status'] == '')         || $where['out_status'] = $data['out_status'];
        empty($data['receiver_name'])   || $where['receiver_name']          =   ['like','%'.$data['receiver_name'] ];
        empty($data['sn'])              || $where['order_sn']               =   $data['sn'];
        empty($data['waybillno'])       || $where['waybillno']              =   $data['waybillno'];

        if( !$this->user_info->isRootUser() ){
            $where['company']   =   $this->user_info->company;
        }

        return OrderGood::where($where)->paginate(10);     
    }
 
    public function getOne($id){
        return OrderGood::get([
        	'rec_id'	=>	$id
        ]);
    }  

    public function getSn(){
        Request::instance()->isPost() || die('request not  post!');
        $param = Request::instance()->param();  //获取参数

        return OrderGood::get([
            'order_sn'    =>  $param['sn']
        ]);
    } 

    public function prints($id){
    	
        return [
        	'info'	=>	$this->getOne($id),
        	'list'	=>	$this->getOne($id)
        ];
    }  

    public function indexAdd($id,$status){
        $info = OrderGood::get(['rec_id'    =>  $id]);
        // $info->out_status = !$info->out_status;
        $info->out_status = $status;
        if( $status == 1 ){
            $nums = -1;
        }else{
            $nums = 1;
        }

        $info->save();

        foreach ($info->getList as $k =>$vo) {
            $goods = Product::get([
                'id'    =>  $vo->findGood->id
            ]);
            $goods->num += $nums;
            $goods->save();
            // dump( $goods );
        }
        
        // dump( $info->getList );
        // die;
        return [
            'error' => 0,
            'msg'   =>  '操作成功'
        ];
    }


    public function packAdd($id){
        $info = OrderGood::get(['rec_id'    =>  $id]);
        $info->pick_status = !$info->pick_status;

        // if( $info->tally_status == 1 ){
        //     return [
        //         'error' => 100,
        //         'msg'   =>  '已经理货打包过了'
        //     ];
        // }

        $info->save();
        return [
            'error' => 0,
            'msg'   =>  '操作成功'
        ];
    }

 
    /**
     * [packSave 打包]
     * @return [array] [结果]
     */
    public function packSave(){

        Request::instance()->isPost() || die('request not  post!');
        $param = Request::instance()->param();  //获取参数

        $info = OrderGood::get(['rec_id'    =>  $param['id']]);
        $info->pick_status      = !$info->pick_status;
        $info->zhouzhuan        = $param['zhouzhuan'];
        $info->shipping_fee     = $param['shipping_fee'];
        $info->receiver_name    = $param['receiver_name'];
        $info->receiver_phone   = $param['receiver_phone'];
        $info->delivery         = $param['delivery'];
        $info->good_num         = $param['good_num'];

        $info->save();
        return [
            'error' => 0,
            'msg'   =>  '操作成功'
        ];
    }   

    public function arrangeAdd($id,$status){
        $info = OrderGood::get(['rec_id'    =>  $id]);
        $info->tally_status = $status;

        $res = $info->save();

        return [
            'error' => 0,
            'msg'   =>  '操作成功'
        ];
    }


}
